package main

import (
	// "replyio"
	"fmt"
	// "net"
	"os"

	"golang.org/x/net/websocket"
)

var url = "ws://127.0.0.1:8080/"
var origin = "http://127.0.0.1:8080"

// 账号登录状态
var tre_login bool = false

func main() {
	//用户信息
	var reply []byte

	ws, _ := websocket.Dial(url, "", origin)

	websocket.Message.Send(ws, "setlogin:330")
	websocket.Message.Send(ws, "990")

	//连接后用户会一直 循环这个
	for {

		//更新用户信息 			理解：错误会返回 信息，此时err值为nil
		if err := websocket.Message.Receive(ws, &reply);
		//不为空，有错误信息
		err != nil {
			return
		}

		//成功返回的标记信息 账号+ok
		if string(reply) == "330ok" {

			tre_login = true
			fmt.Println("等待接收")
			continue
		}

		if tre_login {
			//更新一次 对方发来请求接收的信息 后期修改特殊

			var fileObj, _ = os.OpenFile("b.png", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)

			count, _ := fileObj.Write(reply)
			fileObj.Close()
			fmt.Println(reply, count)

		}

	}

	//ws.Close()
}
